package main

import "fmt"

func main() {
	matrix := [][]byte{{'1', '1', '1', '1', '1'},
		{'1', '1', '1', '1', '1'},
		{'1', '1', '1', '1', '1'},
		{'1', '1', '1', '1', '0'}}
	fmt.Println(maximalSquare(matrix))

}

func maximalSquare(matrix [][]byte) int {
	row := len(matrix)
	column := len(matrix[0])
	maxSquare := 0
	for i := 0; i < row; i++ {
		for j := 0; j < column; j++ {

			valid := true
			if matrix[i][j] != '1' {
				continue
			} else {
				tmpsquare := 1
				limitBorder := min(row-i, column-j)
				for m := 1; m < limitBorder; m++ {
					if matrix[i+m][j+m] == '0' {
						valid = false
						break
					}
					tmpsquare++
					for n := 0; n < m; n++ {
						if matrix[i+n][j+m] == '0' || matrix[i+m][j+n] == '0' {
							valid = false
							break
						}
						tmpsquare += 2
					}
					if valid {
						maxSquare = max(tmpsquare, maxSquare)
					} else {
						break
					}
				}
			}
		}
	}
	return maxSquare
}

func min(arg1, arg2 int) int {
	if arg1 > arg2 {
		return arg2
	}
	return arg1
}

func max(arg1, arg2 int) int {
	if arg1 > arg2 {
		return arg1
	}
	return arg2
}
